#include <tft_lcd.h>
#include <DrvUART.h>
#include <pronano.h>

extern const unsigned char g_Display_blank_output[][12], gDigital_image[][12];

void LCD_Reset(void)
{
  uint8_t TFT_Send_Command[] = { FG035QW00_Initial };
  char TFT_Recv_Data[255];
  int len = 0;

/*  TFT_Send_Command[0] = 0xF1;
  TFT_Send_Command[1] = 0x07;
  
  TFT_Send_Command[2] = TFT_Send_Command[0] + TFT_Send_Command[1];
  TFT_Send_Command[3] = 0x29;
  TFT_Send_Command[4] = 0x00;
  TFT_Send_Command[5] = 0x01;
  TFT_Send_Command[5] = 0x01;
  TFT_Send_Command[TFT_Send_Command[1]] = ;
  TFT_Send_Command[TFT_Send_Command[1]+1] = 0xF4;*/
#ifdef APP_DEBUG
  printf("TFT COMM PORT via UART port: %d\n", TFT_COMM_PORT_ID);
#endif  //APP_DEBUG

  len = sizeof(TFT_Send_Command);
  UART1->FCR.RFR =1;
  DrvUART_Write(TFT_COMM_PORT, TFT_Send_Command, len);

  //Recv_Command(TFT_Recv_Data);
}

void Show_LCD_IMG(const unsigned char img[][12], int digit, int x, int y)
{
  int i = 0;

  memcpy(buf2, img[digit], 12);
  buf2[4] = (x & 0xff00) >> 8;
  buf2[5] = x & 0xff;
  buf2[6] = 0x00;
  buf2[7] = y;
  for (i = 2, buf2[10] = 0; i < 10; i++)
     buf2[10] += buf2[i];
  TFT_Send_Command(buf2, 12, 0);
}


//==============================================================================
//Subroutine:	Show_LCD_Number
//Function:
//==============================================================================
void Show_LCD_Number(char *buf, int start_x, int start_y)
{
  char *str = buf;
  int x = start_x, y = start_y;

  while (*str) {
//    printf("%d\n", *str);
    switch (*str) {
      case '-':
         Show_LCD_IMG(g_Display_blank_output, 22, x, y);
         break;
      case '.':
         Show_LCD_IMG(g_Display_blank_output, 21, x, y);
         break;
	  case '/':
	     Show_LCD_IMG(g_Display_blank_output, 4, x, y);
		 break;
	  case ':':
	     Show_LCD_IMG(g_Display_blank_output, 12, x, y);
	     break;
      default:
	     if (*str != ' ')
           Show_LCD_IMG(gDigital_image, *str-'0', x, y);
         break;
    }
    x -= 8;
    str++;
  }
}